package com.jalarbee.web.multimedia;

import com.jalarbee.storage.http.ParamProtection;

public class FileServletUrlBuilder {

    private String imageServletUrl = "defaultFileServletUrl";

    private String fileServletUrl = "defaultServletUrl";

    private ParamProtection paramProtection;

    public String buildImageUrl(String imageFileId, String type, int width, int height,
            boolean preserveAR, boolean allowEnlarge) {
        StringBuilder sb = new StringBuilder("id=");
        sb.append(imageFileId);
        if (type != null) {
            sb.append("&t=").append(type);
        }
        if (width > 0) {
            sb.append("&scw=").append(width);
        }
        if (height > 0) {
            sb.append("&sch=").append(height);
        }
        if (!preserveAR) {
            sb.append("&scp=0");
        }
        if (allowEnlarge) {
            sb.append("&sce=1");
        }

        String prot = paramProtection.encodeParameters(sb.toString());

        sb = new StringBuilder(imageServletUrl);
        sb.append("?p=").append(prot);
        return sb.toString();
    }

    public String buildFileUrl(String fileId, boolean download) {
        StringBuilder sb = new StringBuilder("id=");
        sb.append(fileId);
        if (download) {
            sb.append("&d=1");
        }

        String prot = paramProtection.encodeParameters(sb.toString());

        sb = new StringBuilder(fileServletUrl);
        sb.append("?p=").append(prot);
        return sb.toString();
    }

    public String buildFileUrl(String fileId) {
        return buildFileUrl(fileId, false);
    }

    public void setParamProtection(ParamProtection value) {
        this.paramProtection = value;
    }

    public void setImageServletUrl(String servletUrl) {
        this.imageServletUrl = servletUrl;
    }

    public void setFileServletUrl(String fileServletUrl) {
        this.fileServletUrl = fileServletUrl;
    }

}
